import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/manager/home'
    },
    {
      path: "/manager",
      component: () => import("@/views/Manager.vue"),
      children: [
        {
          path: 'home',
          redirect: to => {
            const user = JSON.parse(localStorage.getItem('xm-user') || '{}')
            if (user.role === 'ADMIN') {
              return '/manager/admin/home'
            }
            if (user.role === 'PETSHOP') {
              return '/manager/petShop/home'
            }
            return '/login'
          }
        },
        {
          path: "admin/home",
          meta: { name: "管理员系统首页"},
          component: () => import("@/views/manager/AdminHome.vue")
        },
        {
          path: "petShop/home",
          meta: { name: "宠物店系统首页"},
          component: () => import("@/views/manager/PetShopHome.vue")
        },
        {
          path: "admin",
          meta: { name: "管理员信息"},
          component: () => import("@/views/manager/Admin.vue")
        },
        {
          path: "notice",
          meta: { name: "系统公告"},
          component: () => import("@/views/manager/Notice.vue")
        },
        {
          path: "person",
          meta: { name: "个人资料"},
          component: () => import("@/views/manager/Person.vue")
        },
        {
          path: "password",
          meta: { name: "修改密码"},
          component: () => import("@/views/manager/Password.vue")
        },
        {
          path: "petType",
          meta: { name: "宠物类型"},
          component: () => import("@/views/manager/PetType.vue")
        },
        {
          path: "goodsType",
          meta: { name: "宠物用品类型"},
          component: () => import("@/views/manager/GoodsType.vue")
        },
        {
          path: "petShop",
          meta: { name: "宠物店"},
          component: () => import("@/views/manager/PetShop.vue")
        },
        {
          path: "identify",
          meta: { name: "宠物店认证"},
          component: () => import("@/views/manager/Identify.vue")
        },
        {
          path: "user",
          meta: { name: "普通用户"},
          component: () => import("@/views/manager/User.vue")
        },
        {
          path: "pet",
          meta: { name: "宠物"},
          component: () => import("@/views/manager/Pet.vue")
        },
        {
          path: "slideshow",
          meta: { name: "轮播图"},
          component: () => import("@/views/manager/Slideshow.vue")
        },
        {
          path: "goods",
          meta: { name: "宠物用品"},
          component: () => import("@/views/manager/Goods.vue")
        },
        {
          path: "petOrders",
          meta: { name: "宠物订单"},
          component: () => import("@/views/manager/PetOrders.vue")
        },
        {
          path: "goodsOrders",
          meta: { name: "宠物用品订单"},
          component: () => import("@/views/manager/GoodsOrders.vue")
        },
        {
          path: "post",
          meta: { name: "帖子"},
          component: () => import("@/views/manager/Post.vue")
        }
      ]
    },
    {
      path: "/front",
      component: () => import("@/views/Front.vue"),
      children: [
        {
          path: "home",
          component: () => import("@/views/front/Home.vue")
        },
        {
          path: "person",
          component: () => import("@/views/front/Person.vue")
        },
        {
          path: "password",
          component: () => import("@/views/front/Password.vue")
        },
        {
          path: "notice",
          component: () => import("@/views/front/Notice.vue")
        },
        {
          path: "petDetail",
          component: () => import("@/views/front/PetDetail.vue")
        },
        {
          path: "petShopDetail",
          component: () => import("@/views/front/PetShopDetail.vue")
        },
        {
          path: "petShop",
          component: () => import("@/views/front/PetShop.vue")
        },
        {
          path: "pet",
          component: () => import("@/views/front/Pet.vue")
        },
        {
          path: "goods",
          component: () => import("@/views/front/Goods.vue")
        },
        {
          path: "address",
          component: () => import("@/views/front/Address.vue")
        },
        {
          path: "orders",
          component: () => import("@/views/front/Orders.vue")
        },
        {
          path: "cart",
          component: () => import("@/views/front/Cart.vue")
        },
        {
          path: "myPost",
          component: () => import("@/views/front/MyPost.vue")
        },
        {
          path: "forum",
          component: () => import("@/views/front/Forum.vue")
        },
        {
          path: "postDetail",
          component: () => import("@/views/front/PostDetail.vue")
        },
        {
          path: "myComment",
          component: () => import("@/views/front/MyComment.vue")
        },
      ]
    },
    {
      path: '/login',
      component: () => import('@/views/Login.vue')
    },
    {
      path: '/register',
      component: () => import('@/views/Register.vue')
    },
    {
      path: '/404',
      component: () => import('@/views/404.vue')
    },
    {
      path: '/:pathMatch(.*)',
      redirect: '/404'
    }
  ]
})

export default router
